package org.xserver.component.handler.ext.http;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.xserver.component.core.XServerHttpContextAttachment;
import org.xserver.component.core.XServerHttpResponse;
import org.xserver.component.core.dispatch.http.HttpRequestDispatch;
import org.xserver.component.handler.ext.AbstractWriteHandler;
import org.xserver.component.handler.ext.WriteHandler;
import org.xserver.component.handler.ext.WriteHandlerFactory;
import org.xserver.component.handler.ext.http.HttpResponseWriterHandlerFactory.HttpResponseType;

/**
 * Text plain write handler, it use text/plain Http contentType header to write
 * response body.
 * <p>
 * <strong>Note</strong>
 * <ul>
 * <li>2016/07 http interface refactor</li>
 * <li>2016/07 add file response</li>
 * </ul>
 * 
 * @author idoldefine
 * @since 2013/02/02
 * 
 */
public abstract class HttpResponseWriteHandler extends AbstractWriteHandler {

	public static final String CALLBACK = "callback";
	public static final String BRACKET_LEFT = "(";
	public static final String BRACKET_RIGHT = ")";

	@Override
	public Object getContent(XServerHttpContextAttachment attachment, Object obj)
			throws Exception {
		XServerHttpResponse response = attachment.getResponse();
		response.setStatus(HttpResponseStatus.OK);
		response.setContentTypeHeader(getContentType(attachment, obj));
		return doGetContent(attachment, obj);
	}

	protected abstract String getContentType(
			XServerHttpContextAttachment attachment, Object obj);

	protected abstract Object doGetContent(
			XServerHttpContextAttachment attachment, Object obj)
			throws Exception;

	@Override
	protected ChannelFutureListener getListener(
			XServerHttpContextAttachment attachment, Object obj)
			throws Exception {
		return attachment;
	}
}
